package com.zhou.juc.test09_Interview;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @author zhouyanxiang
 * @create 2020-08-2020/8/15-21:09
 */
public class T05_CountDownLatch {

    volatile List lists = new ArrayList();

    public void add(Object o) {
        lists.add(o);
    }

    public int size() {
        return lists.size();
    }

    public static void main(String[] args) {
        T05_CountDownLatch t = new T05_CountDownLatch();

        CountDownLatch countDownLatch = new CountDownLatch(1);

        new Thread(()->{
            System.out.println(Thread.currentThread().getName() + " start... ");
            if (t.size() != 5){
                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + " end... ");
        },"t2").start();



        new Thread(()->{
            System.out.println(Thread.currentThread().getName() + " start... ");
            for (int i = 0; i < 10; i++) {
                t.add(new Object());
                System.out.println("add " + i);
                if (t.size() ==  5){
                    countDownLatch.countDown();
                }
            }
            System.out.println(Thread.currentThread().getName() + " end... ");
        },"t1").start();
    }

}
